package fule.com.playandroidkotlin.ui.activity

import android.content.Intent
import android.view.View
import fule.com.playandroidkotlin.base.BaseActivity
import fule.com.playandroidkotlin.R
import fule.com.playandroidkotlin.base.BaseModel
import fule.com.playandroidkotlin.base.Model
import fule.com.playandroidkotlin.ui.model.RegisterModel
import fule.com.playandroidkotlin.ui.presenter.RegisterPresenter
import fule.com.playandroidkotlin.ui.view.RegisterView
import kotlinx.android.synthetic.main.activity_register.*

/**
 * 作者： njb
 * 时间： 2018/10/26 0026-下午 5:13
 * 描述：
 * 来源：
 */
class RegisterActivity : BaseActivity<RegisterPresenter>(), RegisterView {

    override val layoutId: Int
        get() = R.layout.activity_register

    override fun createPresenter(): RegisterPresenter {
        return RegisterPresenter(this)
    }

    override fun initView() {


    }


    /**
     * 检查输入格式
     *
     * @param phone    用户手机号
     * @param password 密码
     * @return
     */
    private fun checkInput(): Boolean {
        var isRegister = true
        val username = et_username.text.toString()
        val password = et_password.text.toString()
       val  repassword = et_repassword.text.toString()
        if (username.isEmpty()) {
            et_username.error = (getString(R.string.tip_username_can_not_be_empty))
            isRegister = false
        }
        if (password.isEmpty()) {
            et_password.error = (getString(R.string.tip_password_can_not_be_empty))
            isRegister = false
        }
        if (repassword.isEmpty()) {
            et_repassword.error = (getString(R.string.tip_repassword_can_not_be_empty))
            isRegister = false
        }
        if (password != repassword) {
            et_password.error = (getString(R.string.tip_two_password_do_not_match))
            isRegister = false
        }
        return isRegister
    }


    override fun addListener() {
        btn_register.setOnClickListener(onClickListener)
        tv_sign_in.setOnClickListener(onClickListener)
    }

    override fun showLoading() {

    }

    override fun hideLoading() {
    }


    override fun onErrorCode(model: Model) {
        super.onErrorCode(model)
        if(model.errorCode != 0){
            showError("注册失败")
        }else{
            showtoast("注册成功")

        }
    }

    override fun onLoadRegister(o: BaseModel<RegisterModel>?) {
        Intent(context, LoginActivity::class.java).run {
            startActivity(this)
        }

    }


    /**
     * OnClickListener
     */
    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.btn_register -> {
                register()
            }
            R.id.tv_sign_in -> {
                Intent(this@RegisterActivity, LoginActivity::class.java).apply {
                    startActivity(this)
                }
                finish()
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
            }
        }
    }

    /**
     * 注册
     */
    private fun register() {
        if (checkInput()) {
            presenter!!.register(et_username.text.toString(), et_password.text.toString(), et_repassword.text.toString())
        }

    }


}


